<template>
  <div class="home">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区 -->
    <el-card class="mt16">
      <!-- 搜索框 -->
      <el-input
        placeholder="请输入你想要搜索的内容"
        style="width: 350px"
        v-model="shuju.query"
        :clearable="true"
        @input="getlist"
        @clear="getlist"
      >
        <i slot="append" class="el-icon-search" @click="sou"></i>
      </el-input>
      <!-- 搜索框 -->
      <!-- 添加按钮 -->
      <el-button
        type="primary"
        style="margin-left: 15px"
        @click="$router.push('/goodsadd')"
        >添加用户</el-button
      >
      <!-- 添加按钮 -->
      <!-- 表格区 -->
      <el-table :data="user" border style="width: 100%" class="mt16">
        <el-table-column fixed type="index" label="#" width="50">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="380">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column label="创建时间" width="250">
          <template slot-scope="scope">
            <span>{{ scope.row.add_time | gtimer }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="edit(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="del(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格区 -->
      <!-- 分页 -->
      <el-pagination
        @size-change="changesize"
        @current-change="changepage"
        :current-page="shuju.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="shuju.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 分页 -->
      <!-- 编辑的模态框 -->
      <!-- <el-dialog title="编辑用户" :visible.sync="dialogFormVisible1">
        <el-form
          :model="form1"
          :rules="rules"
          ref="edusers"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="goods_name">
            <el-input
              v-model="form1.goods_name"
              style="width: 100%"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="goods_price">
            <el-input
              v-model="form1.goods_price"
              style="width: 100%"
            ></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="goods_number">
            <el-input
              v-model="form1.goods_number"
              style="width: 100%"
            ></el-input>
          </el-form-item>
          <el-form-item label="重量" prop="goods_weight">
            <el-input
              v-model="form1.goods_weight"
              style="width: 100%"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="xg">确定</el-button>
        </div>
      </el-dialog> -->
      <!-- 编辑的模态框 -->
    </el-card>
  </div>
</template>

<script>
import { goodslist } from '@/http/api.js'
import { delgoods } from '@/http/api.js'
import _ from 'lodash'
export default {
  data() {
    return {
      user: [], //存放的数据
      shuju: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      dialogFormVisible1: false,
      form1: {
        goods_name: '',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
        id: 0
      },
      rules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        goods_cat: [
          { required: true, message: '请输入商品分类', trigger: 'blur' }
        ]
      }
    }
  },
  // filters: {
  //   getime(val) {
  //     console.log('过滤器', val)
  //     return new Date(val).toLocaleString().replace(/\//g, '-')
  //   }
  // },
  mounted() {
    this.getlist()
  },
  methods: {
    //   请求的数据
    async getlist() {
      const res = await goodslist(this.shuju)
      console.log('商品列表的数据', res)
      this.user = res.goods
      this.total = res.total
    },
    // 删除
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log(id)
          const res = await delgoods(id)
          console.log('删除goods', res)
          this.getlist()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 改变每页多少条数
    changesize(val) {
      this.shuju.pagesize = val
      this.getlist()
    },
    // 改变页码数
    changepage(val) {
      this.shuju.pagenum = val
      this.getlist()
    },
    // 防抖
    sou: _.debounce(function () {
      this.getlist()
    }, 500),
    // 编辑
    edit(row) {
      console.log('编辑row', row)
      this.$router.push({
        path: '/goodsedit',
        query: { row }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.home {
  width: 100%;
}
</style>
